package com.uchengwang.ui.cm_ui.me.ui;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;

import com.chanven.lib.cptr.PtrClassicFrameLayout;
import com.chanven.lib.cptr.PtrDefaultHandler;
import com.chanven.lib.cptr.PtrFrameLayout;
import com.chanven.lib.cptr.recyclerview.RecyclerAdapterWithHF;
import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.config.UserConfig;
import com.uchengwang.functions.http.entity.MapRequest;
import com.uchengwang.functions.http.listener.HttpCallBack;
import com.uchengwang.functions.http.manager.HttpManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.login.bean.UserBean;
import com.uchengwang.ui.cm_ui.me.adapter.MembersAdapter;
import com.uchengwang.ui.utils.JsonUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import java.util.List;


/**
 * Created by U城网 on 2017/9/10.
 */

public class ClassMemberActivity extends BaseWithTitleActivity{
    private RecyclerView recycleView;
    private PtrClassicFrameLayout mSwipeRefreshLayout;
    private RecyclerAdapterWithHF mAdapter;
    private MembersAdapter adapter;
    private int page;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_ustudy_2);
        setTitle("班级成员");
        initView();
    }

    private void initView(){
        recycleView = (RecyclerView) findViewById(R.id.u_study_recycleView);
        mSwipeRefreshLayout = (PtrClassicFrameLayout) findViewById(R.id.mRefreshLayout);

        initData();

    }

    private void initData(){
        adapter = new MembersAdapter(this, 0);

        mAdapter = new RecyclerAdapterWithHF(adapter);
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.setAdapter(mAdapter);

        mSwipeRefreshLayout.postDelayed(new Runnable() {

            @Override
            public void run() {
                mSwipeRefreshLayout.autoRefresh(true);
            }
        }, 150);

        mSwipeRefreshLayout.setPtrHandler(new PtrDefaultHandler() {

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                page = 1;
                getClassMembers();
            }
        });

    }

    private void getClassMembers(){
        MapRequest map = new MapRequest();
        map.put(BaseConfig.CLASS_ID, UChengApplication.uniDataId);
        HttpManager.newInstance(this).query(UserConfig.GetClassStu.method, UserConfig.GetClassStu.URL, map, new HttpCallBack() {
            @Override
            public void onError(int status, String msg) {
                Log.d("jjg", "stutas="+status+"=msg="+msg);
                mSwipeRefreshLayout.refreshComplete();

                UCommonUtil.getUtil().showToast(ClassMemberActivity.this, "获取失败");

            }

            @Override
            public void onResp(int status, String msg, String data) {
                Log.d("jjg", "stutas="+status+"=msg="+msg);
                mSwipeRefreshLayout.refreshComplete();

                if (!TextUtils.isEmpty(data) && !data.equals("[]")){
                    List<UserBean> list = JsonUtils.getManager().parseList(data, UserBean.class);
                    adapter.setData(list);
                }
            }
        });


    }
}
